﻿using Knlght.Model.Sys;
using Knlght.Service;
using Knlght.Web.Infrastructure;
using Knlght.Web.Infrastructure.Menus;
using Knlght.Web.Infrastructure.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Knlght.Domain;
using Knlght.Web.Areas.Manage.Controllers.Filters;

namespace Knlght.Web.Areas.Manage.Controllers
{
    public class AdministratorsController : BaseAdminController
    {
        [MallAdminMenu("System_Administrators")]
        [Permission("System_Administrators")]
        public ActionResult Index()
        {
            var service = new RoleInfoService();
            var query = new QueryRequest<RoleInfo>();
            query.Add(o => o.IsEnabled == true);
            var rsp = service.GetList(query);
            if (rsp.IsSuccess)
            {
                ViewBag.RoleInfoList = rsp.Models;
            }
            return View();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdatePwd()
        {
            return View();
        }

        #region AngularJs
        const string AD_UPLOAD_DIRECTORY = "administrator";
        /// <summary>
        /// 上传文件
        /// </summary>
        [HttpPost]
        public JsonResult Upload()
        {
            var rsp = this.UploadPics(AD_UPLOAD_DIRECTORY);
            return EJItemJson(rsp);
        }
        [HttpPost]
        public JsonResult Paging(PagingParameter parameter)
        {
            var query = new QueryRequest<Administrators>();
            if (parameter.Query.CreateCriterionByString(nameof(Administrators.Username)) != null)
            {
                var username = parameter.Query.CreateCriterionByString(nameof(Administrators.Username)).Value.ToString();
                query.Add(O => O.Username.Contains(username));
            }
            if (parameter.Query.CreateCriterionByString(nameof(Administrators.RealName)) != null)
            {
                var realName = parameter.Query.CreateCriterionByString(nameof(Administrators.RealName)).Value.ToString();
                query.Add(O => O.RealName.Contains(realName));
            }
            if (parameter.Query.CreateCriterionByString(nameof(Administrators.Email)) != null)
            {
                var email = parameter.Query.CreateCriterionByString(nameof(Administrators.Email)).Value.ToString();
                query.Add(O => O.Email.Contains(email));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(Administrators.IsEnabled)) != null)
            {
                var isEnabled = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(Administrators.IsEnabled)).Value);
               query.Add(o => o.IsEnabled == isEnabled);
            }
            query.Add(o => o.IsSuper == false);     
            query.AddOrder(nameof(Administrators.AddTime),true);
            query.AddOrder(nameof(Administrators.Phone),false);     
            var service = new AdministratorsService();            
            var pagingRequest = new PagingRequest<Administrators>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query        
            };
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }
        [HttpPost]
        public JsonResult Add(Administrators model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = new AdministratorsService();
            var rsp = service.Add(model);        
            if (rsp.IsSuccess)
            {
                return EJActionJson(true, model.Id);
            }
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Update(Administrators model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = new AdministratorsService();
            var rsp = service.Update(model);          
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Updates(string[] ids, string property, object value)
        {         
            if (property.ToLower() == nameof(Administrators.IsEnabled).ToLower())
            {
                var service = new AdministratorsService();
                var request = new QueryRequest<Administrators>();
                request.Add(o => ids.Contains(o.Id));
                var rsp = service.Update(request, o=>new Administrators { IsEnabled=(bool)value});               
                return EJActionJson(rsp);
            }           
            else
            {
                return EJActionJson(false, "该属性修改方法没有实现！");
            }
        }
        [HttpPost]
        public JsonResult Remove(string id)
        {
            var service = new AdministratorsService();
            var query = new QueryRequest<Administrators>();
            query.Add(o => o.Id == id);
            var rsp = service.Remove(query);            
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Removes(string[] ids)
        {
            var service = new AdministratorsService();
            var query = new QueryRequest<Administrators>();
            query.Add(o => ids.Contains(o.Id));
            var rsp = service.Remove(query);           
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult UpdatePassword(string oldPwd, string newPwd, string confirmPwd)
        {
            if (newPwd != confirmPwd)
            {
                return EJActionJson(false, "新密码和确认新密码不一致！");
            }
            if (oldPwd == newPwd)
            {
                return EJActionJson(false, "旧密码和新密码不能一致！");
            }
            var user = GetUser();
            if (user != null)
            {
                if (oldPwd != Dxeciphering(user.Password))
                {
                    return EJActionJson(false, "旧密码有误！");
                }
                user.Password = newPwd;
                var service = new AdministratorsService();
                var rsp = service.Update(user);              
                return EJActionJson(rsp);
            }
            return EJActionJson(false, "账号不存在！");
        }
        #endregion
    }
}